package cn.wx.jove.core.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wx.jove.core.entity.UserPlate;
import cn.wx.common.persistence.base.BaseService;
import cn.wx.jove.core.mapper.UserPlateMapper;
import cn.wx.common.persistence.exception.MyException;
import cn.wx.common.persistence.web.ResponseCode;

/**
 * <p>
 * 用户和板块关系表 服务类
 * </p>
 *
 * @author
 * @since 2018-11-30
 */
@Service
@Transactional
public class UserPlateService extends BaseService<UserPlate, UserPlateMapper> {

	/**
	 * 新增用户和板块关系表
	 * 
	 * @param userPlate
	 * @return
	 * @throws MyException
	 */
	public boolean create(UserPlate userPlate) throws MyException {
		return insert(userPlate);
	}

	/**
	 * 修改用户和板块关系表
	 * 
	 * @param userPlate
	 * @return
	 * @throws MyException
	 */
	public boolean modify(UserPlate userPlate) throws MyException {
		// PID 不能为空
		if (userPlate.getpId() == null) {
			throw new MyException(ResponseCode.PARAMETER_REQUIRED);
		}
		return update(userPlate);
	}

	/**
	 * 删除用户和板块关系表
	 * 
	 * @param userPlate
	 * @return
	 * @throws MyException
	 */
	public boolean remove(UserPlate userPlate) throws MyException {
		// PID 不能为空
		if (userPlate.getpId() == null) {
			throw new MyException(ResponseCode.PARAMETER_REQUIRED);
		}
		userPlate.setfRemove(true);
		return update(userPlate);
	}

	/**
	 * 更新用户板块的x/y和width/height
	 * 
	 * @param userPlate
	 * @return
	 * @throws MyException
	 */
	public boolean savePosition(List<UserPlate> userPlates) throws MyException {
		for(UserPlate userPlate : userPlates) {
			if (userPlate.getpId() == null) {
				throw new MyException(ResponseCode.PARAMETER_REQUIRED);
			}
			update(userPlate);
		}
		return true;
	}
}
